﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MS.InformationManagement.Presenter.MenuLateral;
using System.Collections.Generic;
using MS.InformationManagement.Controls;
using MS.InformationManagement.Presenter.Support;
using System.Text;

namespace MS.InformationManagement.WP.DocumentCenter.MenuLateral
{
    public partial class MenuLateralUserControl : BaseControl, IMenuLateralView
    {

        #region Fields

        MenuLateralPresenter _presenter;

        #endregion

        #region Properties

        public Dictionary<string, Dictionary<string, string>> Items {get; set;}

        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new MenuLateralPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();

            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            try
            {
                menuContainer.InnerHtml = getMenuItems();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region Private Methods

        private string getMenuItems()
        {
            StringBuilder menu = new StringBuilder();
            menu.Append("<ul class='accordion'  id='accordion-3'>");
            foreach (var menuItem in Items)
            {
                if (menuItem.Value == null)
                    menu.Append(this.getSingleLi("#", menuItem.Key,"root-menu-item"));
                else
                    menu.Append(this.getRootLi(menuItem));
            }
            menu.Append("</ul>");
            return menu.ToString();
        }

        private string getRootLi(KeyValuePair<string, Dictionary<string, string>> items)
        {
            StringBuilder li = new StringBuilder();      
            li.Append(string.Format("<li class='root-menu-item'>{0}<ul>", getAnchor(items.Key,"#")));
            li.Append(this.getMultLi(items.Value)); 
            li.Append("</ul></li>");
            return li.ToString();
        }

        private string getMultLi(Dictionary<string,string> items)
        {
            StringBuilder li = new StringBuilder();
            foreach (var item in items)
            {
                li.Append(this.getSingleLi(item.Value,item.Key,"single-menu-item"));
            }
            return li.ToString();
        }

        private string getSingleLi(string url, string title, string className)
        {
            StringBuilder li = new StringBuilder();
            li.Append(string.Format("<li class='{1}'>{0}</li>", getAnchor(title, url),className));
            return li.ToString();
        }    

        private string getAnchor(string title, string url)
        {
            url = Page.Request.Url.AbsoluteUri + "?" + url;
            return string.Format("<a href='{0}'>{1}</a>", url, title);
        }

        #endregion
    }
}
